/**
 * refer to http://developer.mixi.co.jp/appli/spec/pc/get_mymixi_info#toc-1
 */
var APP_ID = 32211;
var nickname = "";
var GAME_TIME = 4;
var gameTime;
var PRIVACY_POST="privacy";
var SERVER_NAME="http://thedestinytoday.appspot.com/";

function staticPageRequest(pageID){
	var REQUEST_URL = SERVER_NAME + pageID;
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
 
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	gadgets.io.makeRequest(REQUEST_URL, function(response){
		/// no action
		if (response.data) {
		
			document.getElementById("mainContents").innerHTML = response.data;
			document.getElementById("mainContents").style.backgroundImage = 'url("")';
			
		}
		else {
			var err = response.errors[0];
			window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
		}
	}, params);
}

function privacyRequest(){
	var REQUEST_URL = SERVER_NAME + PRIVACY_POST;
	var params = {};
	params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
 
	params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
	gadgets.io.makeRequest(REQUEST_URL, function(response){
		/// no action
		if (response.data) {
		
			document.getElementById("mainContents").innerHTML = response.data;
			document.getElementById("mainContents").style.backgroundImage = 'url("")';
			
		}
		else {
			var err = response.errors[0];
			window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
		}
	}, params);
}

function createTopPage(){
    var url = SERVER_NAME+"game";
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
    params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
    params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
    
    gadgets.io.makeRequest(url, function(response){
        if (response.data) {
        
            document.getElementById("mainContents").innerHTML = response.data;
            //		document.getElementById("mainContents").style.backgroundImage = 'url("http://thedestinytoday.appspot.com/html/top760.JPG")';
            gameTime = 0;
            getUserInfo();
            
        }
        else {
            var err = response.errors[0];
            window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
        }
    }, params);
}

function createGamePage(){
    var REQUEST_URL = SERVER_NAME+"game";
    var params = {};
    params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
    params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
    
    //detect gender
    var gender = "1";
    if (document.getElementsByName("gender")[0].checked) 
        gender = "0";
    
    var post_data = {
        questionNum: document.getElementById("questionNum").value,
        sessionID: document.getElementById("sessionID").value,
        nickname: document.getElementById("nickname").value,
        gender: gender
    };
    params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(post_data);
    gadgets.io.makeRequest(REQUEST_URL, function(response){
        /// no action
        if (response.data) {

            document.getElementById("mainContents").innerHTML = response.data;
  //          document.getElementById("hoge").innerHTML = "game";
              gameTime = gameTime + 1;
            if (gameTime > GAME_TIME) {
                document.getElementById("mainContents").style.backgroundImage = 'url("'+ SERVER_NAME+'html/result760.PNG")';
    //            document.getElementById("nickname").value = nickname;
				document.getElementById("nickNameArea").innerHTML=nickname+"さんの";
            }
            else {
                document.getElementById("mainContents").style.backgroundImage = 'url("'+ SERVER_NAME+'html/game760.PNG")';
                
            }          
            
            
            
        }
        else {
            var err = response.errors[0];
            window.alert("タイムアウトしました。しばらく時間をおいてからリロードしてください。");
        }
    }, params);
}

function getUserInfo(){
	
	var req = opensocial.newDataRequest();
    var params = {};
    params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.HAS_APP];
    req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER, params), "viewer");
    req.send(function(data){
        if (!data.hadError()) {
            var item = data.get("viewer");
            if (item.hadError()) {
                var code = item.getErrorCode();
                // error logic                
            }
            else {
               		
				var viewer = item.getData();
                hasApp = viewer.getField(opensocial.Person.Field.HAS_APP);
                // if viewer doesn't have application, move to application registration page
                if (hasApp == "false") {
					location.href = "http://mixi.jp/view_appli.pl?id=" + APP_ID;
				}
				else {
					nickname = viewer.getDisplayName();
					document.getElementById("nicknameArea").innerHTML = nickname + "さん";
					document.getElementById("nickname").value = nickname;
				}
            }
        }
    });
}




/**
 *  refer to http://developer.mixi.co.jp/appli/spec/pc/invite_appli
 */
function inviteFriends(){
    //var params = {};
    //params[opensocial.IdSpec.Field.USER_ID] = inviteFriendIDs;
    //var recipients = opensocial.newIdSpec(params);
    //opensocial.requestShareApp(recipients, null, function(response){
    opensocial.requestShareApp("VIEWER_FRIENDS", null, function(response){
        if (response.hadError()) {
            var errCode = response.getErrorCode();
        }
        else {
            var recipientIds = response.getData()["recipientIds"];
        }
    });
}


/**
 * refer to http://developer.mixi.co.jp/appli/spec/pc/send_voice
 * @param {Object} index
 */
function doVoice(voiceText){
    var params = {};
    // When you want to add URL in voice, please 
    params[mixi.Status.Field.URL] = "http://mixi.jp/view_appli.pl?id=" + APP_ID + "&appParams=";
    params[mixi.Status.Field.MOBILE_URL] = "http://ma.mixi.net/" + APP_ID + "/?url=";
    params[mixi.Status.Field.TOUCH_URL] = "http://mixi.jp/view_appli.pl?id=" + APP_ID + "&appParams=";
    mixi.requestUpdateStatus(voiceText, function(response){
        if (response.hadError()) {
            var code = response.getErrorCode();
            var msg = response.getErrorMessage();
        }
        else {
        
        }
    }, params);
}


